package action;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;

import entity.User;
import service.UserDAO;
import service.impl.UserDAOImpl;

public class UserAction extends SuperAction implements ModelDriven<User>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user=new User();
	private String errmsg;
	
	//用户登录动作
	public String login()
	{
		UserDAO udao=new UserDAOImpl();
		if(udao.userLogin(user))
		{
			session.setAttribute("loginUserName", user.getPwd());
			setErrmsg("");
			return "login_success";
		}
		else
		{
			setErrmsg("账号或密码输入错误");
			return "login_failure";
		}
	}
	public String getErrmsg(){
		return errmsg;
	}
	public void setErrmsg(String errmsg){
		this.errmsg=errmsg;
	}
	
	
//	@SkipValidation
	public String logout()
	{
		if(session.getAttribute("loginUserName")!=null)
		{
			session.removeAttribute("loginUserName");
		}
		return "logout_success";
	}
	

//	@Override
//	public void validate() {
//		// TODO Auto-generated method stub
//		if("".equals(String.valueOf(user.getUid()).trim()))
//		{
//			this.addFieldError("uidError", "账号不能为空");
//		}
//		if("".equals(user.getPwd().trim()))
//		{
//			this.addFieldError("pwdError", "密码不能为空");
//		}
//	}
	public User getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}
	
	public String query()
	{
		UserDAO udao=new UserDAOImpl();
		List<User> list=udao.queryAllUser();
		//放进session中
		if(list!=null&&list.size()>0)
		{
			session.setAttribute("user_list",list);			
		}
		return "query_success";
	}
	
	//删除用户动作
	public String delete()
	{
		UserDAO udao=new UserDAOImpl();
		String uid=request.getParameter("uid");
		udao.deleteUser(uid);
		return "delete_success";
	}
	
	//添加用户动作
	public String add() throws Exception
	{
		User u=new User();
		u.setUname(request.getParameter("uname"));
		u.setBankcno(request.getParameter("bankcno"));
		u.setType(request.getParameter("type"));
		u.setCompany(request.getParameter("company"));
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//		u.setDate(sdf.parse(request.getParameter("date")));
		UserDAO udao=new UserDAOImpl();
		udao.addUser(u);
		return "add_success";
	}
	
	//修改用户资料动作
	public String modify() 
	{
		//获得传递过来的用户编号
		String uid=request.getParameter("uid");
		UserDAO udao=new UserDAOImpl();
		User u=udao.queryUserByUid(uid);
		//保存在会话中
		session.setAttribute("modify_user", u);
		return "modify_success";
	}
	
	//保存修改后的用户资料动作
	public String save() throws Exception
	{
		User u=new User();
		u.setUid(request.getParameter("uid"));
		u.setPwd(request.getParameter("pwd"));
		u.setUname(request.getParameter("uname"));
		u.setBankcno(request.getParameter("bankcno"));
		u.setType(request.getParameter("type"));
		u.setCompany(request.getParameter("company"));
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//		u.setDate(sdf.parse(request.getParameter("date")));
		UserDAO udao=new UserDAOImpl();
		udao.updateUser(u);
		return "save_success";
	}
	
	
	
	
}
